package com.ms.oschina.base;

import android.R.bool;
import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.util.LogUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.ActionBarDrawerToggle;
import com.ms.oschina.view.DrawerArrowDrawable;

/**
 * 1,规范代码结构 2,提供公用方法，精简代码
 */
public abstract class BaseActivity extends SwipeBackActivity implements
		View.OnClickListener {
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle toggle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
		View view = getContentView();
		if (view == null) {
			setContentView(getLayoutId());
		} else {
			setContentView(view);
		}

		initView();
		initEvent();
		initData();
		registerCommonBtn();

		if (!isFullScreen()) {
			setActionBar();
		}

		setCanGestureSwipe(isCanSwipe());
	}
	
	protected Boolean isCanSwipe() {
		return true;
	}

	protected boolean isFullScreen() {
		return false;
	}

	private void setActionBar() {
		// 判断是否是主页，
		// 是主页显示 是主页： 显示主页标题栏，不是显示带有返回的标题栏

		if (isHomeActivity()) {
			setHomeActionbar();
		} else {
			setBackActionBar();
		}
	}

	private void setBackActionBar() {
		ActionBar mActionBar = getActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(true);

		// 自定义actionbarView
		mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);// 只有设置这个参数自定义的actionbarView才会生效
		View view = UiUtils.inflate(R.layout.back_actionbar);
		view.findViewById(R.id.back).setOnClickListener(this);
		TextView tv = (TextView) view.findViewById(R.id.back_actionbar_title);
		tv.setText(getBackActionBarTitle());
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		mActionBar.setCustomView(view, params);
	}

	/**
	 * 如果子类想要修改Actionbar标题，覆盖此方法
	 * 
	 * @return
	 */
	protected CharSequence getBackActionBarTitle() {
		return UiUtils.getString(R.string.app_name);
	}

	private void setHomeActionbar() {
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		toggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				new DrawerArrowDrawable(this) {
					@Override
					public boolean isLayoutRtl() {
						return false;// false 菜单在左边
					}
				}, 0, 0) {
			public void onDrawerClosed(View view) {
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(toggle);
	}

	public DrawerLayout getmDrawerLayout() {
		return mDrawerLayout;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (toggle != null) {
			if (toggle.onOptionsItemSelected(item)) {
				return true;
			}
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// 需要将ActionDrawerToggle与DrawerLayout的状态同步
		// 将ActionBarDrawerToggle中的drawer图标，设置为ActionBar中的Home-Button的Icon
		if (toggle != null) {
			toggle.syncState();
		}
	}

	protected View getContentView() {
		return null;
	}

	public void init() {
	}

	/**
	 * 在多个界面间公用的按钮，在此处注册点击事件
	 */
	private void registerCommonBtn() {
		View view = findViewById(R.id.back);
		if (view != null) {
			view.setOnClickListener(this);
		}
	}

	/**
	 * 返回当前Activity使用的布局ID
	 */
	protected int getLayoutId() {
		return 0;
	}

	/**
	 * 只执行findViewById操作
	 */
	protected void initView() {

	}

	/**
	 * 注册监听器，适配器
	 */
	protected void initEvent() {

	}

	/**
	 * 获取数据，填充界面
	 */
	protected void initData() {

	}

	/**
	 * BaseActivity没有处理的点击事件，在此方法处理
	 */
	protected void processClick(View v) {

	}

	protected boolean isHomeActivity() {
		return false;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		default:
			processClick(v);
			break;
		}
	}

}
